package com.runfast.waimai.util;


import com.runfast.waimai.dao.model.RunfastBusiness;
import com.runfast.waimai.dao.model.RunfastBusinessAccounts;
import com.runfast.waimai.service.RunfastBusinessAccountsService;
import com.runfast.waimai.service.RunfastBusinessService;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.*;

//import com.runfast.waimai.service.RunfastSensitiveService;敏感词类


@Scope("singleton")
@Component("runFastSettingts")
public class RunFastSettingts {
	//正在处理接单操作的订单id
	private Set<Integer> takeThatOrderIdSet = new HashSet<Integer>();
	/*@Resource
	private RunfastSensitiveService sensitiveService;*/
	@Resource
	private RunfastBusinessAccountsService businessAccountsService;
	@Resource
	private RunfastBusinessService businessService;
	private Set<String> sensitiveSet;
	
	private Map<String,RunfastBusinessAccounts> map=new HashMap<String,RunfastBusinessAccounts>();
	private Date lastTime;
	public Set<String> getSensitiveSet() {
		return sensitiveSet;
	}

	public void setSensitiveSet(Set<String> sensitiveSet) {
		this.sensitiveSet = sensitiveSet;
	}
	/**
	 * 判断是否有人正在执行该订单的接单操作
	 * @param orderId 订单id
	 * */
	public boolean hasTakeThatOrderId(Integer orderId){
		return takeThatOrderIdSet.contains(orderId);
	}
	/**
	 * 添加正在接单操作的订单id
	 * @param orderId 订单id
	 * */
	public synchronized void  addTakeThatOrderId(Integer orderId){
		takeThatOrderIdSet.add(orderId);
	}
	/**
	 * 删除正在接单操作的订单id
	 * @param orderId 订单id
	 * */
	public synchronized void  removeTakeThatOrderId(Integer orderId){
		takeThatOrderIdSet.remove(orderId);
	} 
	@PostConstruct
	/*public void init(){
		sensitiveSet = new HashSet<String>();
		List<Sensitive> list = sensitiveService.findAll();
		if(list != null && list.size() > 0){
			for(Sensitive s:list){
				String[] ssss = s.getSensitiveWords().split("\\|");
				for(String key:ssss){
					sensitiveSet.add(key);
				}
			}
		}
		setting();
	}*/
	//清空内存再查找
/*	public void redoCantion(){
		sensitiveSet.clear();
		
		List<Sensitive> list = sensitiveService.findAll();
		lastTime=new Date();
		if(list != null && list.size() > 0){
			for(Sensitive s:list){
				String[] ssss = s.getSensitiveWords().split("\\|");
				for(String key:ssss){
					sensitiveSet.add(key);
				}
			}
		}
	}*/
	/**
	 * 判断是否包含有敏感词
	 * @param content
	 * */
	/*public boolean isCantion(String content){
		boolean isok = false;
		
		*//*if(new Date().getTime()-lastTime.getTime()>7200000){
		//	redoCantion();
		}*//*
		for(String s:sensitiveSet){
			if(content.indexOf(s)!=-1){//12345 2 ->1 -->-1
				isok = true;
				break;
			}
			
		}
		return isok;
	}*/
	private void setting(){
		
	}	
	public String getsents(String cont){
			
			 if(sensitiveSet!=null&&sensitiveSet.size()>0){
				 for(String senStr:sensitiveSet){
					 if(!ComUtil.isEmpty(cont)){
						 if(cont.indexOf(senStr)!=-1){
							 if(senStr.length()>0){
								 
								 String star="";
								 	for(int i=0;i<senStr.length();i++){
								 		star+="*";
								 	}
								 	cont=cont.replaceAll(senStr, star);
							 }
						 }
						 
					 }
					 
				 }
				
			 }
		return cont;
		
	}

	
	public Map<String, RunfastBusinessAccounts> getMap() {
		if(map==null){
			map=new HashMap<String, RunfastBusinessAccounts>();
		}
		return map;
	}

	public void setMap(Map<String, RunfastBusinessAccounts> map) {
		this.map = map;
	}

	public void redoBusinessAccounts(){
		map.clear();
		List<RunfastBusinessAccounts> list = businessAccountsService.findAll();
		//查询所有的商家账号信息
		lastTime=new Date();
		if(list != null && list.size() > 0){
			for(RunfastBusinessAccounts acc:list){
				if(acc!=null&&acc.getBusinessid()!=null){
					RunfastBusiness bus=businessService.findById(acc.getBusinessid());
					//查询商家信息
					if(bus!=null){
						acc.setIspush(bus.getIsdeliver());//设置是不是接收推送
						map.put(acc.getAccounts(),acc);
					}
					
				}
			}
		}
	}
}
